<?php

echo "<img src = \"assignhead.jpg\" alt = \"Assignments!\" /><br>";

require_once ('assignmentManager.php');
require_once ('courseManager.php');

function generateAssignmentList() {
    $assignArray = array();
    $assignArray = assignmentManager::getIncompleteAssignments($_SESSION['userID']);
    $default = "<table>";
    foreach ($assignArray as $assign) {
        $default = $default . "<tr><td valign=\"top\">" . $assign->getName() . "</td><td valign=\"top\"><a href=\"index.php?complete=assign&id=" . $assign->getAssignID() . "\">Complete</a></td><td valign=\"top\"><a href=\"index.php?edit=assign&id=" . $assign->getAssignID() . "\">Edit</a></td></tr>";
    }
    $default = $default . "</table><br><br>";

    $default = $default . "<a href=index.php?add=assign>Add Assignment</a> | <a href=index.php?view=assign>View All Assignments</a>";

    return $default;
}

function generateAllAssignmentsList() {
    $assignArray = array();
    $assignArray = assignmentManager::getAllAssignments($_SESSION['userID']);
    $default = "<table>";
    foreach ($assignArray as $assign) {
        if ($assign->getIsComplete() == "0")
            $complete = "<a href=\"index.php?complete=assign&id=" . $assign->getAssignID() . "\">Complete</a>";
        $default = $default . "<tr><td valign=\"top\">" . $assign->getName() . "</td><td valign=\"top\">$complete</td><td valign=\"top\"><a href=\"index.php?edit=assign&id=" . $assign->getAssignID() . "\">Edit</a></td></tr>";
    }
    $default = $default . "</table><br><br>";

    return $default;
}

function generateAssignForm($action) {

    if (isset($_GET['id'])) {
        $assignment = assignmentManager::getAssignment($_GET['id']);
        $name = " value=\"" . $assignment->getName() . "\"";
        $description = " value=\"" . $assignment->getDescription() . "\"";
        $courseIDfirst = $assignment->getClassID();
        $weightIDfirst = $assignment->getWeight();
        $dueDate = $assignment->getDateDue();
        $dueYear = " value=\"" . substr($dueDate, 0, 4) . "\"";
        $dueMonth = " value=\"" . substr($dueDate, 5, 2) . "\"";
        $dueDay = " value=\"" . substr($dueDate, 8, 2) . "\"";
        $alert = $assignment->getAlert();
        $isComplete = $assignment->getIsComplete();
        $earned = $assignment->getEarnedPoints();
        $potent = $assignment->getPotentialPoints();
        if ($alert == "1")
            $alert = " checked";
        else
            $alert = "";
    }
    else {
        $dueYear = " value=\"YYYY\"";
        $dueMonth = " value=\"MM\"";
        $dueDay = " value=\"DD\"";
    }

    $courseArray = array();
    $courseArray = courseManager::getAllCourses($_SESSION['userID']);
    $courseSelect = "<option value=\"0\">No Course</option>";
    $weightSelect = "<option value=\"0\">No Weight</option>";
    foreach ($courseArray as $course) {

        $courseID = $course->getID();
        if ($courseID == $courseIDfirst)
            $courseDefault = " selected";
        else
            $courseDefault = "";

        $courseName = $course->getName();
        $courseSelect = $courseSelect . "<option value=\"$courseID\" $courseDefault>$courseName</option>";

        $weightArray = weightManager::getWeights($courseID);
        foreach ($weightArray as $weight) {
            $weightID = $weight->getWeightID();
            $weightName = $weight->getType();
            $weightPercent = $weight->getPercent();
            if ($weightID == $weightIDfirst)
                $weightDefault = " selected";
            else
                $weightDefault = "";
            $weightSelect = $weightSelect . "<option value=\"" . $weightID . "\" $weightDefault>$courseName: $weightName - $weightPercent%</option>";
        }
    }

    $weightInput = "<select name=\"weight\">$weightSelect</select>";

    if (isset($_GET['courseid'])) {
        $courseInfo = courseManager::getCourse($_GET['courseid']);
        $courseInput = $courseInfo->getName() . "<input type=\"hidden\" value=\"" . $courseInfo->getID() . "\" name=\"course\">";
    } else {
        $courseInput = "<select name=\"course\">$courseSelect</select>";
    }

    if ($isComplete == "1") {
        $completePortion = "
            <tr><td>Earned Points: </td><td><input type=\"text\" name=\"earnedPoints\" value=\"$earned\"></td></tr>
            <tr><td>Potential Points: </td><td><input type=\"text\" name=\"potentPoints\" value=\"$potent\"></td></tr>
            ";
    }

    $form = "
        <form method=\"POST\" action=\"$action\">
        <table>
        <tr><td>*Assignment name:</td><td><input type=\"text\" name=\"name\" $name></td></tr>
        <tr><td>Description:</td><td><input type=\"textarea\" name=\"description\" $description></td></tr>
        <tr><td>*Course:</td><td>$courseInput</td></tr>
        <tr><td>Weight:</td><td><input type=\"radio\" name=\"weight\" value=\"add\" onclick=\"enableWeightAdd()\"> Add or
        <input type=\"radio\" name=\"weight\" value=\"choose\" checked onclick=\"enableWeightChoose()\"> Choose Existing</td></tr>
        </table>
        <div id=\"addWeight\" name=\"addWeight\" style=\"display:none\">
        Weight Type: <input type=\"text\" name=\"weightName\"><br>
        Percentage: <input type=\"text\" name=\"weightPercent\">%<br>
        </div>
        
        <div id=\"chooseWeight\" name=\"chooseWeight\" style=\"display:none\">
        $weightInput
        </div>
        <table>
        <tr><td>Due date:</td><td><input type=\"text\" name=\"dueMonth\" size=\"2\" $dueMonth><input type=\"text\" name=\"dueDay\" size=\"2\" $dueDay><input type=\"text\" name=\"dueYear\" size=\"4\" $dueYear></td></tr>
        $completePortion
        <tr><td>Alert:</td><td><input type=\"checkbox\" value=\"1\" name=\"alert\" $alert>Yes</td></tr>
        </table>
        <input type=\"submit\" name=\"submit\" value=\"Submit\">
        </form>
    ";
    return $form;
}

$default = generateAssignmentList();

if ($add == "assign") {
    if ($submit == "1") {
        if (!isset($_POST['name'])) {
            echo "You must enter a name for the assignment.";
            echo $assignForm;
        } else {
            if ($_POST['weight'] == "choose") {
                $weight = weightManager::getWeight($_POST['weight']);
            } else if ($_POST['weight'] == "add") {
                $weight = new Weight();
                $weight->setCourseID($_POST['course']);
                $weight->setPercent($_POST['weightPercent']);
                $weight->setType($_POST['weightName']);
                $weight = weightManager::addWeight($weight);
                $weightID = $weight->getWeightID();
            }
            $wtCheckID = $weight->getCourseID();
            $crseCheckID = $_POST['course'];

            if ($wtCheckID == $crseCheckID || $wtCheckID == "0") {
                $newAssign = new Assignment();
                $name = $_POST[name];
                $newAssign->setName($name);
                $newAssign->setClassID($_POST['course']);
                $newAssign->setDescription($_POST['description']);
                if ($_POST['alert'] == "1")
                    $newAssign->setAlert($_POST['alert']);
                else
                    $newAssign->setAlert(0);
                $newAssign->setIsComplete(0);

                $newAssign->setWeight($_POST['weight']);
                $newAssign->setEarnedPoints(0);
                $newAssign->setPotentialPoints(0);
                $newAssign->setDateDue($_POST[dueYear] . "-" . $_POST['dueMonth'] . "-" . $_POST['dueDay']);
                $newAssign->setUserID($_SESSION[userID]);
                assignmentManager::addAssignment($newAssign);
                echo "<br><br>";
                $default = generateAssignmentList();
                echo $default;
            } else {
                echo "You must choose a weight from the course selected.";
                $action = "index.php?add=assign&submit=1";
                echo generateAssignForm($action);
            }
        }
    } else {
        $action = "index.php?add=assign&submit=1";
        echo generateAssignForm($action);
    }
} else if ($edit == "assign") {
    if (isset($_GET['id'])) {
        $assign = new Assignment();
        $assign = assignmentManager::getAssignment($_GET['id']);
        $name = $assign->getName();
        $description = $assign->getDescription();

        echo "<a href=index.php?delete=assign&id=" . $_GET['id'] . ">Delete assignment</a> or<br>";
        $action = "index.php?edit=assign&id=" . $_GET['id'] . "&submit=1";
        echo generateAssignForm($action);

        if ($submit == "1") {
            if (isset($_POST['name'])) {

                if ($_POST['weight'] == "choose") {
                    $weight = weightManager::getWeight($_POST['weight']);
                } else if ($_POST['weight'] == "add") {
                    $weight = new Weight();
                    $weight->setCourseID($_POST['course']);
                    $weight->setPercent($_POST['weightPercent']);
                    $weight->setType($_POST['weightName']);
                    $weight = weightManager::addWeight($weight);
                    $weightID = $weight->getWeightID();
                }
                $wtCheckID = $weight->getCourseID();
                $crseCheckID = $_POST['course'];

                if ($wtCheckID == $crseCheckID || $wtCheckID == "0") {

                    $editAssign = new Assignment();
                    $name = $_POST[name];
                    $editAssign->setName($name);
                    $editAssign->setDescription($_POST['description']);
                    $editAssign->setAlert($_POST['alert']);
                    if (isset($_POST['earnedPoints']) && isset($_POST['potentPoints'])) {
                        $editAssign->setIsComplete(1);
                        $editAssign->setEarnedPoints($_POST['earnedPoints']);
                        $editAssign->setPotentialPoints($_POST['potentPoints']);
                    } else {
                        $editAssign->setIsComplete(0);
                    }
                    $editAssign->setWeight($_POST['weight']);
                    $editAssign->setDateDue($_POST[dueYear] . "-" . $_POST['dueMonth'] . "-" . $_POST['dueDay']);
                    $editAssign->setUserID($_SESSION[userID]);
                    assignmentManager::editAssignment($editAssign);
                    echo "<br><br>";
                    $default = generateAssignmentList();
                    echo $default;
                } else {
                    echo "You must choose a weight from the course selected.";
                    $action = "index.php?edit=assign&id=" . $_GET['id'] . "&submit=1";
                    echo generateAssignForm($action);
                }
            } else {
                echo "You must enter a name for the assignment.";
                echo $assignForm;
            }
        } else {
            echo $assignForm;
        }
    } else {
        echo "You must select an assignment to edit.";
        echo $assignForm;
    }
} else if ($delete == "assign") {
    if (isset($_GET['id'])) {
        $assignDelete = assignmentManager::getAssignment($_GET['id']);
        if ($submit == "1") {
            assignmentManager::deleteAssignment($assignDelete);
            $default = generateAssignmentList();
            echo $default;
        } else {
            echo "Are you sure you wish to delete this assignment?<br><br>";
            $name = $assignDelete->getName();
            $description = $assignDelete->getDescription();
            echo "
                $name <br>
                $description <br><br>
                <a href=\"index.php?delete=assign&id=$_GET[id]&submit=1\">Yes</a> or <a href=\"index.php\">No</a>
                ";
        }
    } else {
        echo "You must select an assignment to delete.";
        echo $default;
    }
} else if ($complete == "assign") {
    if (isset($_GET['id'])) {
        $completeForm = "
            <form method=\"POST\" action=\"index.php?complete=assign&id=" . $_GET['id'] . "&submit=1\">
            <table>
            <tr><td>Earned Points: </td><td><input type=\"text\" name=\"earned\"></td></tr>
            <tr><td>Potential Points: </td><td><input type=\"text\" name=\"potential\"></td></tr>
            </table>
            <input type=\"submit\" value=\"Submit\">
            ";
        if ($submit == "1") {
            if (isset($_POST['earned']) && isset($_POST['potential'])) {
                $assign = assignmentManager::getAssignment($_GET['id']);

                $earned = $_POST['earned'];
                $potential = $_POST['potential'];

                $assign->setEarnedPoints($earned);
                $assign->setPotentialPoints($potential);
                $assign->setIsComplete(1);
                assignmentManager::editAssignment($assign);
                $default = generateAssignmentList();
                echo $default;
            } else {
                echo "You must complete the form.<br>";
                echo $completeForm;
            }
        } else {
            echo $completeForm;
        }
    } else {
        echo "You must enter an assignment to complete.";
        echo $default;
    }
} else if ($_GET['view'] == "assign") {
    echo generateAllAssignmentsList();
} else {
    $default = generateAssignmentList();
    echo $default;
}
?>
